package com.huishen.edrivenew.ui;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import com.huishen.edrivenew.Constants;
import com.huishen.edrivenew.R;
import com.huishen.edrivenew.base.BaseActivity;
import com.huishen.edrivenew.net.JsonResponseHandler;
import com.huishen.edrivenew.net.NetApi;
import com.huishen.edrivenew.util.StringUtils;

/**
 * 
 */
public class ChangePwdActivity extends BaseActivity {
    
    @InjectView(R.id.header_title)
    public TextView title;
    @InjectView(R.id.header_back)
    public ImageButton back;
    
    @InjectView(R.id.verify_edit_number)
    public EditText userName;
    @InjectView(R.id.verify_edit_vcode)
    public EditText passWord;
    @InjectView(R.id.new_pwd)
    public EditText newPwd;
    @InjectView(R.id.re_pwd)
    public EditText rePwd;
    
    @InjectView(R.id.verify_btn_start)
    public Button loginBtn;
    
    
    private String t_username;
    private String t_password;
    private String t_newpassword;
    private String t_repassword;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.activity_changepwd);
        ButterKnife.inject(this);
        
        initView();
        
    }
    
    
    private void initView() {
        title.setText("修改密码");
    }
    
    
    
    @OnClick(R.id.verify_btn_start)
    public void login() {
        t_username = userName.getText().toString().trim();
        t_password = passWord.getText().toString().trim();
        t_newpassword = newPwd.getText().toString().trim();
        t_repassword = rePwd.getText().toString().trim();
        
        if(StringUtils.isEmpty(t_username)) {
            showToast("请输入注册帐号");
            return;
        }
        if(StringUtils.isEmpty(t_password)) {
            showToast("请输入旧密码");
            return;
        }
        if(StringUtils.isEmpty(t_newpassword)) {
            showToast("请输入新密码");
            return;
        }
        if(StringUtils.isEmpty(t_repassword)) {
            showToast("请确认新密码");
            return;
        }
        if(!t_newpassword.equals(t_repassword)) {
            showToast("两次输入新密码不一致");
            return;
        }
        
        showDialog();
        loginRequest(t_username, t_password, t_newpassword);
    }
    
    
    private void loginRequest(String uName, String pwd, String newpwd) {
        NetApi.changePassWord(this, new JsonResponseHandler() {
            @Override
            public void onSuccess(int code, JSONObject jsonObj, Object[] args) {
                dismissDialog();
                try {
                    if(code == Constants.N_PWD_ERROR) {
                    } else if(code == Constants.N_SUCCESS) {
                        showToast("修改成功！");
                        toMain();
                    } else {
                        showToast("修改失败！");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    showToast("修改失败！");
                }
            }
            @Override
            public void onFailure(int code, String errorMessage, Object[] args) {
                dismissDialog();
                showToast("修改失败！");
            }
            @Override
            public void onSessionFailure() {
                dismissDialog();
                super.onSessionFailure();
            }
        }, uName, pwd, newpwd);
    }
    
    
    @OnClick(R.id.header_back)
    public void toMain() {
        Intent intent = new Intent(this, LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        this.startActivity(intent);
        this.finish();
    }
    
}
